package com.nektos.ccf.server;

import javax.persistence.EntityManager;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.nektos.ccf.client.VolunteerService;
import com.nektos.ccf.shared.Volunteer;

public class VolunteerServiceImpl extends RemoteServiceServlet implements VolunteerService {

	@Override
	public void create(Volunteer volunteer) throws IllegalArgumentException {
        
		validate(volunteer);
        
        EntityManager em = EMF.get().createEntityManager();
        try {
            em.persist(volunteer);
        } catch (IllegalArgumentException ex) {
            throw ex;
        } catch (Exception ex) {
        	ex.printStackTrace();
        	throw new RuntimeException(ex.getMessage());
        } finally {
        	em.close();
        }

	}
    
	private void validate(Volunteer volunteer) throws IllegalArgumentException {
        if(volunteer == null)
    		throw new IllegalArgumentException("Volunteer is null");
        else if(volunteer.getFullName()==null || volunteer.getFullName().length()==0)
    		throw new IllegalArgumentException("Full Name is required.");
        else if(volunteer.getEmail()==null || volunteer.getEmail().length()==0)
    		throw new IllegalArgumentException("Email is required.");
	}
    
}
